package com.yzion.baselib.widget.titlebar

import android.app.Activity
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.yzion.baselib.R
import com.yzion.baselib.databinding.ByTitleBarBinding
import com.yzion.baselib.view.BaseTitleBar

/**
 * 默认样式的通用标题栏，布局左，中，右
 *
 * 左布局：1个 ImageView + 1个 TextView ，一般用于点击返回
 * 中间布局：1个 ImageView + 1个 TextView ，一般用于标题
 * 右布局：1个 ImageView + 1个 TextView ，一般用于设置，保存，更多等，跳转按钮
 *
 * 如果不能满足需求，则可以自定义标题栏，自定义全局默认标题栏需要继承BaseTitleBar，
 * 然后在App中的 createTitleBarView 实现全局初始化，注意：App需继承BaseApp
 * ZJJ
 */
open class DefaultTitleBar : BaseTitleBar {

    lateinit var binding: ByTitleBarBinding

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        if (isInEditMode) {
            return
        }
        binding = ByTitleBarBinding.bind(view)
        initProperty(context, attrs)
        setDefaultAttr()
        setMode(MODE_PURE_TITLE)
    }

    override fun getDefaultBarBackground(): Drawable? {
        return if (titleBarDefaultAttr != null && titleBarDefaultAttr!!.background() != null) {
            titleBarDefaultAttr!!.background()
        } else {
            ColorDrawable(titleBarDefaultAttr!!.backgroundColor())
        }
    }

    //设置全局默认值
    private fun setDefaultAttr() {
        titleBarDefaultAttr?.let {
            setTitleTextColor(it.titleTextColor())
            setLeftTextColor(it.leftTextColor())
            setRightTextColor(it.rightTextColor())
            setTitleTextSize(it.titleTextSizeDp())
            setLeftTextSize(it.leftTextSizeDp())
            setRightTextSize(it.rightTextSizeDp())
            setRightIcon(it.rightIcon())
            setLeftIcon(it.leftIcon())
            setCenterIcon(it.centerIcon())
            setBarHeight(it.titleBarHeightDp())
        }
    }

    //设置XML传值
    private fun initProperty(context: Context, attrs: AttributeSet?) {
        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView)
            val titleStr = typedArray.getString(R.styleable.TitleBarView_title_text)
            val rightText = typedArray.getString(R.styleable.TitleBarView_right_text)
            val leftText = typedArray.getString(R.styleable.TitleBarView_left_text)
            val leftIconId = typedArray.getResourceId(R.styleable.TitleBarView_left_icon, 0)
            val rightIconId = typedArray.getResourceId(R.styleable.TitleBarView_right_icon, 0)
            val centerIconId = typedArray.getResourceId(R.styleable.TitleBarView_center_icon, 0)
            val bottomDividerEnable =
                typedArray.getBoolean(R.styleable.TitleBarView_bottom_divider_enable, false)
            val titleTextColor = typedArray.getColor(
                R.styleable.TitleBarView_title_color,
                ColorUtils.getColor(R.color.color_black)
            )
            val rightTextColor = typedArray.getColor(
                R.styleable.TitleBarView_right_text_color,
                ColorUtils.getColor(R.color.color_666666)
            )
            val leftTextColor = typedArray.getColor(
                R.styleable.TitleBarView_left_text_color,
                ColorUtils.getColor(R.color.color_666666)
            )

            setTitleTextColor(titleTextColor)
            setRightTextColor(rightTextColor)
            setLeftTextColor(leftTextColor)

            setRightText(rightText)
            setTitleText(titleStr)
            setLeftText(leftText)

            if (leftIconId != 0) {
                setLeftIcon(leftIconId)
            } else {
                setLeftIconVisible(false)
            }
            if (rightIconId != 0) {
                setRightIcon(rightIconId)
            } else {
                setRightIconVisible(false)
            }
            if (centerIconId != 0) {
                setCenterIcon(centerIconId)
            } else {
                setCenterIconVisible(false)
            }
            setBottomLineVisible(bottomDividerEnable)
            typedArray.recycle()
        }
    }

    /**
     * 设置模式
     *
     * @param mode DefaultTitleBar.MODE_BACKPRESS 带返回按钮的标题栏
     * ,DefaultTitleBar.MODE_PURE_TITLE 只有标题
     * @return
     */
    fun setMode(mode: Int): DefaultTitleBar {
        when (mode) {
            MODE_BACK_PRESS -> {
                binding.llTitleBarLeft.visibility = VISIBLE
                setLeftIconVisible(true)
                binding.llTitleBarLeft.setOnClickListener {
                    if (context is Activity) {
                        KeyboardUtils.hideSoftInput((context as Activity))
                        (context as Activity).onBackPressed()
                    }
                }
            }
            MODE_PURE_TITLE -> {
                setLeftIconVisible(false)
                setLeftTextVisible(false)
                setRightIconVisible(false)
                setRightTextVisible(false)
            }
        }
        return this
    }

    /**
     * 设置标题栏底部分割线是否显示
     *
     * @param Visible
     * @return
     */
    fun setBottomLineVisible(b: Boolean): DefaultTitleBar {
        binding.line.visibility = if (b) VISIBLE else GONE
        return this
    }

    fun setTitleText(title: String?): DefaultTitleBar {
        binding.tvTitle.text = title
        return this
    }

    fun setTitleTextColor(@ColorInt color: Int): DefaultTitleBar {
        binding.tvTitle.setTextColor(color)
        return this
    }

    fun setTitleTextSize(dp: Int): DefaultTitleBar {
        binding.tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, dp.toFloat())
        return this
    }

    fun setLeftText(text: String?): DefaultTitleBar {
        binding.tvTitleBarLef.text = text
        binding.tvTitleBarLef.visibility = VISIBLE
        return this
    }

    fun setLeftTextColor(@ColorInt color: Int): DefaultTitleBar {
        binding.tvTitleBarLef.setTextColor(color)
        return this
    }

    fun setLeftTextSize(dp: Int): DefaultTitleBar {
        binding.tvTitleBarLef.setTextSize(TypedValue.COMPLEX_UNIT_DIP, dp.toFloat())
        return this
    }

    fun setLeftIcon(@DrawableRes resId: Int): DefaultTitleBar {
        binding.ivLeftIcon.setImageResource(resId)
        binding.ivLeftIcon.visibility = VISIBLE
        return this
    }

    fun setLeftIcon(drawable: Drawable?): DefaultTitleBar {
        binding.ivLeftIcon.setImageDrawable(drawable)
        binding.ivLeftIcon.visibility = VISIBLE
        return this
    }

    fun setLeftTextVisible(b: Boolean): DefaultTitleBar {
        binding.tvTitleBarLef.visibility = if (b) VISIBLE else GONE
        return this
    }

    fun setLeftIconVisible(b: Boolean): DefaultTitleBar {
        binding.ivLeftIcon.visibility = if (b) VISIBLE else GONE
        return this
    }

    fun setOnLeftClickListener(onLeftIconClickListener: OnClickListener?): DefaultTitleBar {
        binding.llTitleBarLeft.setOnClickListener(onLeftIconClickListener)
        return this
    }

    fun setRightText(text: String?): DefaultTitleBar {
        binding.tvTitleBarRight.text = text
        binding.tvTitleBarRight.visibility = VISIBLE
        return this
    }

    fun setRightTextColor(@ColorInt color: Int): DefaultTitleBar {
        binding.tvTitleBarRight.setTextColor(color)
        return this
    }

    fun setRightTextSize(dp: Int): DefaultTitleBar {
        binding.tvTitleBarRight.setTextSize(TypedValue.COMPLEX_UNIT_DIP, dp.toFloat())
        return this
    }

    fun setRightIcon(@DrawableRes resId: Int): DefaultTitleBar {
        binding.ivRightIcon.setImageResource(resId)
        binding.ivRightIcon.visibility = VISIBLE
        return this
    }

    fun setRightIcon(drawable: Drawable?): DefaultTitleBar {
        binding.ivRightIcon.setImageDrawable(drawable)
        binding.ivRightIcon.visibility = VISIBLE
        return this
    }

    fun setCenterIcon(@DrawableRes resId: Int): DefaultTitleBar {
        binding.ivCenterIcon.setImageResource(resId)
        binding.ivCenterIcon.visibility = VISIBLE
        return this
    }

    fun setCenterIcon(drawable: Drawable?): DefaultTitleBar {
        binding.ivCenterIcon.setImageDrawable(drawable)
        binding.ivCenterIcon.visibility = VISIBLE
        return this
    }

    fun setCenterIconVisible(b: Boolean): DefaultTitleBar {
        binding.ivCenterIcon.visibility = if (b) VISIBLE else GONE
        return this
    }

    fun setRightTextVisible(b: Boolean): DefaultTitleBar {
        binding.tvTitleBarRight.visibility = if (b) VISIBLE else GONE
        return this
    }

    fun setRightIconVisible(b: Boolean): DefaultTitleBar {
        binding.ivRightIcon.visibility = if (b) VISIBLE else GONE
        return this
    }

    fun setOnRightClickListener(onRightClickListener: OnClickListener?): DefaultTitleBar {
        binding.llTitleBarRight.setOnClickListener(onRightClickListener)
        return this
    }

    fun setOnCenterClickListener(onClick: OnClickListener?): DefaultTitleBar {
        binding.ivCenterIcon.setOnClickListener(onClick)
        binding.tvTitle.setOnClickListener(onClick)
        return this
    }

    /**
     * 设置标题栏高度
     *
     * @param dpHeight
     */
    fun setBarHeight(dpHeight: Int): DefaultTitleBar {
        binding.rlLayout.layoutParams.height = ConvertUtils.dp2px(dpHeight.toFloat())
        return this
    }

    /**
     * 设置标题栏背景颜色
     */
    override fun setBarBackgroundColor(color: Int): DefaultTitleBar {
        return super.setBarBackgroundColor(color) as DefaultTitleBar
    }

    /**
     * 设置标题栏背景图片
     */
    override fun setBarBackground(drawable: Drawable?): DefaultTitleBar {
        return super.setBarBackground(drawable) as DefaultTitleBar
    }

    /**
     * 设置标题栏背景透明度，多用于滚动时标题背景的淡出淡入
     */
    override fun setBarBackgroundAlpha(alpha: Int): DefaultTitleBar {
        return super.setBarBackgroundAlpha(alpha) as DefaultTitleBar
    }

    override fun getLayoutId() = R.layout.by_title_bar

    /**
     * 全局默认值属性提供，一般在App中初始化，通过DefaultTitleBar.setTitleBarDefaultAttr()设置
     */
    interface TitleBarDefaultAttr {

        @ColorInt
        fun backgroundColor(): Int //标题栏背景颜色(纯色) ColorUtils.getColor(R.color.xxx)

        @ColorInt
        fun titleTextColor(): Int //标题文本颜色

        @ColorInt
        fun leftTextColor(): Int //左边文本的颜色

        @ColorInt
        fun rightTextColor(): Int //右边文本的颜色

        //标题栏背景颜色(可设置图片或者渐变)，优先级比backgroundColor高
        // ResourceUtils.getDrawable(R.drawable.xxx)
        fun background(): Drawable?
        fun rightIcon(): Drawable? //右边图标
        fun leftIcon(): Drawable? //左边图标
        fun centerIcon(): Drawable? //中间图标

        fun titleTextSizeDp(): Int //标题文本大小单位(dp)
        fun leftTextSizeDp(): Int //左边文本大小单位(dp)
        fun rightTextSizeDp(): Int //右边文本大小单位(dp)
        fun titleBarHeightDp(): Int //标题栏高度单位(dp)

    }

    companion object {
        /**
         * 返回按钮+标题
         */
        const val MODE_BACK_PRESS = 1

        /**
         * 纯标题
         */
        const val MODE_PURE_TITLE = 2

        private var titleBarDefaultAttr: TitleBarDefaultAttr? = null

        /**
         * 全局 TitleBarView 标题颜色和字体大小配置
         *
         * @param attr
         */
        fun setTitleBarDefaultAttr(attr: TitleBarDefaultAttr?) {
            titleBarDefaultAttr = attr
        }
    }
}